<?php

namespace Admin\Controller;
use Think\Controller;

class OrderController extends BaseController{
	
	
	
	public function array_sort($array, $key){
		if(is_array($array)){
			$key_array = null;
			$new_array = null;
			for($i=0;$i<count($array);$i++){
				$key_array[$array[$i][$key]] = $i;
			}
			krsort($key_array);
			$j=0;
			foreach($key_array as $k=>$v){
				$new_array[$j] = $array[$v];
				$j++;
			}
			unset($key_array);
			return $new_array;
		}else{
			return $array;
		}
	}
	
	public function index(){
		$start = I('starttime');
		$end = I('endtime');
		if($strat){
			$start = date("Ymd",strtotime($start));
			$map['miner_date'] = array('egt',$start);
		}
		if($end){
			$end = date("Ymd",strtotime($end));
			$map['miner_date'] = array('elt',$end);
		}
		if($start && $end){
			$start = date("Ymd",strtotime($start));
			$end = date("Ymd",strtotime($end));
			$map['miner_date'] = array(array('egt',$start),array('elt',$end),'AND');
		}
		
		$miner = M('miner');
		$count = $miner->where($map)->count();
		$page = new \Think\Page($count,10);
		$show = $page->show();
		$list1 = $miner->where($map)->limit($page->firstRow.','.$page->listRows)->order("miner_id DESC")->select();
		//var_dump($list1);exit();
		for($i=0;$i<count($list1);$i++){
			$user = M('user')->where("id = {$list1[$i]['user_id']}")->find();
			$list1[$i]['user_dsq_money'] = round($list1[$i]['user_dsq_money'], 2);
			$list1[$i]['nickname'] = $user['nickname'];
			if($list1[$i]['user_id']){
				$list1[$i]['user'] = M('user')->where("id = {$list1[$i]['user_id']}")->find();
			}else{
				$list1[$i]['user'] = M('user')->where("id = {$list1[$i]['user_id']}")->find();
			}
		}
		
		$this->assign('starttime',$start);
		$this->assign('endtime',$end);
		$this->assign('list',$list1);
		$this->assign('show',$show);
		$this->display();
	}
	
	
	//每日输赢
	public function win_lose(){
		$time = I('time');
		$admin = session('admin');
		if($admin['role_id'] != 0){
			$map['tid'] = $admin['uid'];
		}
		if($time){
			$start = strtotime($time.'00:00:00');
			$end = strtotime($time.'23:59:59');
			$map['time'] = array(array('egt',$start),array('elt',$end),'and');
		}else{
			$time = date('Y-m-d');
			$start=mktime(0,0,0,date('m'),date('d'),date('Y'));
			$end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
			$map['time'] = array(array('egt',$start),array('elt',$end),'and');
		}
		$order = M('order');
        $del = $order->where($map)->sum('add_points');
        $add = $order->where($map)->sum('del_points');
		
		$this->assign('del',$del);
		$this->assign('add',$add);
		$this->assign('time',$time);
		$this->display();
	}
	
	
	//每日输赢
	public function user_win_lose(){
		$nickname = I('nickname');
		$time = I('time');
		$admin = session('admin');
		if($admin['role_id'] != 0){
			$map['tid'] = $admin['uid'];
		}
		if($nickname){
			$map['nickname'] = array("LIKE", '%' . $nickname . '%');
		}
		if($time){
			$start = strtotime($time.'00:00:00');
			$end = strtotime($time.'23:59:59');
			$map['time'] = array(array('egt',$start),array('elt',$end),'and');
		}else{
			$time = date('Y-m-d');
			$start=mktime(0,0,0,date('m'),date('d'),date('Y'));
			$end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
			$map['time'] = array(array('egt',$start),array('elt',$end),'and');
		}
        
		$order = M('order');
        $list = $order->where($map)->select();
        $arr = array();
        foreach($list as $k => $v){
            if(!in_array($v['userid'],$arr)){
               $arr[] = $v['userid'];
            }
        }
        
        if($_SESSION['admin']['login_type'] == 0){
            foreach($arr as $k => $v){
                $nicknames = D('user')->where('id = %d', $v)->getField('nickname');
                $data[$k]['add'] = 0;
                $data[$k]['del'] = 0;
                foreach($list as $key => $value){
                    if($v == $value['userid']){
                        $data[$k]['userid'] = $v;
                        $data[$k]['nickname'] = $nicknames;
                        $data[$k]['add'] += $value['add_points'];
                        $data[$k]['del'] += $value['del_points'];
                    }
                }
            }
            $add = $order->where($map)->sum('add_points');
            $del = $order->where($map)->sum('del_points');
        }else{
            foreach($arr as $k => $v){
                $userinfo = D('user')->where('id = %d', $v)->find();
                if($userinfo['t_id'] == $_SESSION['admin']['uid']){
                    $data[$k]['add'] = 0;
                    $data[$k]['del'] = 0;
                    foreach($list as $key => $value){
                        if($v == $value['userid']){
                            $data[$k]['userid'] = $v;
                            $data[$k]['nickname'] = $userinfo['nickname'];
                            $data[$k]['add'] += $value['add_points'];
                            $data[$k]['del'] += $value['del_points'];
                        }
                    }
                }
            }
        }
		

		$this->assign('nickname',$nickname);
		$this->assign('list',$data);
		$this->assign('time',$time);
		$this->assign('add',$add);
		$this->assign('del',$del);
		$this->display();
	}
    
    public function excel(){
        set_time_limit(0);
        $admin = session('admin');
        if($admin['role_id'] != 0){
        	$map['tid'] = $admin['uid'];
        }
        $orderlist = D('order')->where($map)->order('time desc')->select();
        $data = array();
        foreach ($orderlist as $k=>$v){
            $data[$k]['nickname'] = $v['nickname'];
            $data[$k]['number'] = $v['number'];
            $data[$k]['time'] = date('Y-m-d H:i:s',$v['time']);
            $data[$k]['jincai'] = $v['jincai'];
            $data[$k]['add_points'] = $v['add_points'];
            $data[$k]['del_points'] = $v['del_points'];
            $data[$k]['balance'] = $v['balance'];
        }
        foreach ($data as $field=>$v){
            if($field == 'nickname'){
                $headArr[]='用户名';
            }
            if($field == 'number'){
                $headArr[]='期号';
            }
            if($field == 'time'){
                $headArr[]='时间';
            }
            if($field == 'jincai'){
                $headArr[]='竞猜';
            }
            if($field == 'add_points'){
                $headArr[]='进项';
            }
            if($field == 'del_points'){
                $headArr[]='出项';
            }
            if($field == 'balance'){
                $headArr[]='余额';
            }
        }
        $filename="order_list";
        $this->getExcel($filename,$headArr,$data);
    }
    
    
    private  function getExcel($fileName,$headArr,$data){
        //导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Writer.Excel5");
        import("Org.Util.PHPExcel.IOFactory.php");
        
        $date = date("Y_m_d",time());
        $fileName .= "_{$date}.xls";
        
        //创建PHPExcel对象，注意，不能少了\
        $objPHPExcel = new \PHPExcel();
        $objProps = $objPHPExcel->getProperties();
        
        //设置表头
        $key = ord("A");
        foreach($headArr as $v){
            $colum = chr($key);
            $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'1', $v);
            $key += 1;
        }
        
        $column = 2;
        $objActSheet = $objPHPExcel->getActiveSheet();
        foreach($data as $key => $rows){ //行写入
            $span = ord("A");
            foreach($rows as $keyName=>$value){// 列写入
                $j = chr($span);
                $objActSheet->setCellValue($j.$column, $value);
                $span++;
            }
            $column++;
        }
        
        $fileName = iconv("utf-8", "gb2312", $fileName);
        
        //重命名表
        //$objPHPExcel->getActiveSheet()->setTitle('test');
        
        //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $objPHPExcel->setActiveSheetIndex(0);
        
        //清除缓冲区,避免乱码
        ob_end_clean();
        
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载
        exit;
    }
	
	
}


?>